import { ref, computed } from 'vue'
import type { Achievement } from '../types/social'

interface AchievementCriteria {
  id: string
  name: string
  description: string
  icon: string
  maxProgress: number
  condition: (stats: any) => number
  reward?: {
    type: 'rating' | 'title' | 'badge'
    value: string | number
  }
}

export function useAchievementSystem() {
  const achievements = ref<Achievement[]>([])
  const unlockedAchievements = computed(() => 
    achievements.value.filter(a => a.completed)
  )

  const achievementCriteria: AchievementCriteria[] = [
    {
      id: 'first-win',
      name: '初战告捷',
      description: '赢得第一场对局',
      icon: '🏆',
      maxProgress: 1,
      condition: (stats) => Math.min(stats.wins, 1),
      reward: {
        type: 'rating',
        value: 50
      }
    },
    {
      id: 'win-streak',
      name: '连胜大师',
      description: '取得5连胜',
      icon: '🔥',
      maxProgress: 5,
      condition: (stats) => Math.min(stats.winStreak, 5),
      reward: {
        type: 'title',
        value: '连胜大师'
      }
    },
    {
      id: 'checkmate-master',
      name: '将杀专家',
      description: '通过将杀获得10场胜利',
      icon: '⚔️',
      maxProgress: 10,
      condition: (stats) => Math.min(stats.checkmates, 10),
      reward: {
        type: 'badge',
        value: 'checkmate-master'
      }
    },
    {
      id: 'quick-win',
      name: '闪电战',
      description: '在15步内获胜',
      icon: '⚡',
      maxProgress: 1,
      condition: (stats) => stats.quickWins,
      reward: {
        type: 'rating',
        value: 100
      }
    },
    {
      id: 'endgame-expert',
      name: '残局专家',
      description: '在只剩5个棋子的情况下获胜',
      icon: '🎯',
      maxProgress: 5,
      condition: (stats) => Math.min(stats.endgameWins, 5),
      reward: {
        type: 'title',
        value: '残局专家'
      }
    },
    {
      id: 'opening-master',
      name: '开局大师',
      description: '使用10种不同的开局获胜',
      icon: '📚',
      maxProgress: 10,
      condition: (stats) => Math.min(stats.uniqueOpeningWins, 10),
      reward: {
        type: 'badge',
        value: 'opening-master'
      }
    }
  ]

  const loadAchievements = () => {
    const savedAchievements = localStorage.getItem('chessAchievements')
    if (savedAchievements) {
      achievements.value = JSON.parse(savedAchievements)
    } else {
      achievements.value = achievementCriteria.map(criteria => ({
        id: criteria.id,
        name: criteria.name,
        description: criteria.description,
        icon: criteria.icon,
        progress: 0,
        maxProgress: criteria.maxProgress,
        completed: false,
        reward: criteria.reward
      }))
      saveAchievements()
    }
  }

  const saveAchievements = () => {
    localStorage.setItem('chessAchievements', JSON.stringify(achievements.value))
  }

  const checkAchievements = (stats: any) => {
    let newlyUnlocked = false

    achievements.value.forEach(achievement => {
      if (!achievement.completed) {
        const criteria = achievementCriteria.find(c => c.id === achievement.id)
        if (criteria) {
          const progress = criteria.condition(stats)
          achievement.progress = progress

          if (progress >= achievement.maxProgress && !achievement.completed) {
            achievement.completed = true
            achievement.completedAt = new Date()
            newlyUnlocked = true
          }
        }
      }
    })

    if (newlyUnlocked) {
      saveAchievements()
    }

    return newlyUnlocked
  }

  const getProgress = (achievementId: string) => {
    const achievement = achievements.value.find(a => a.id === achievementId)
    if (!achievement) return 0
    return (achievement.progress / achievement.maxProgress) * 100
  }

  const getUnlockedRewards = () => {
    return unlockedAchievements.value
      .filter(a => a.reward)
      .map(a => a.reward!)
  }

  return {
    achievements,
    unlockedAchievements,
    loadAchievements,
    checkAchievements,
    getProgress,
    getUnlockedRewards
  }
} 